<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: 当燃      
 * Date: 2015-09-21
 */

namespace app\admin\controller;
use think\Db;
use think\Page;
use app\admin\logic\GoodsLogic;
use app\common\model\Goods;

class AdText extends Base{
    public function ad(){       
        $act = I('get.act','add');
        $ad_id = I('get.ad_id/d');
        $is_app_ad = I('get.is_app_ad/d');//是否APP广告

        $ad_info = array();
        if($ad_id){
            $ad_info = D('ad_text')->where('ad_id',$ad_id)->find();
             $ad_info['start_time'] = date('Y-m-d',$ad_info['start_time']);
            $ad_info['end_time'] = date('Y-m-d',$ad_info['end_time']);     
        }

        $this->assign('info',$ad_info);
        $this->assign('act',$act);
        return $this->fetch();
    }
    
    public function adList(){
        delFile(RUNTIME_PATH.'html'); // 先清除缓存, 否则不好预览
        $Ad =  M('adText');         
        $keywords = I('keywords/s',false,'trim');
        if(I('ad_top') !== ''){
            $where['ad_top'] = I('ad_top');
            $ad_top = I('ad_top');
        }

        if($keywords){
            $where['ad_name'] = array('like','%'.$keywords.'%');
        }
        $count = $Ad->where($where)->count();// 查询满足要求的总记录数
        $Page = $pager = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $res = $Ad->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        // $res = $Ad->where($where)->limit(2)->select();
        // var_dump($res);
        // exit;                    
        $show = $Page->show();// 分页显示输出
        $this->assign('list',$res);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$pager);     
        $this->assign('ad_top',$ad_top);     

        //判断API模块存在
        if(is_dir(APP_PATH."/api")) $this->assign('is_exists_api',1);
        return $this->fetch();
    }
    

    
    public function adHandle(){
    	$data = I('post.');
    	$data['start_time'] = strtotime($data['begin']);
    	$data['end_time'] = strtotime($data['end']);
    	
    	if($data['act'] == 'add'){
    		$r = D('ad_text')->add($data);
    	}
    	if($data['act'] == 'edit'){
    		$r = D('ad_text')->where('ad_id', $data['ad_id'])->save($data);
    	}
    	
    	if($data['act'] == 'del'){
            $r = D('ad_text')->where('ad_id', $data['del_id'])->delete();
            if($r){
                $this->ajaxReturn(['status'=>1,'msg'=>"操作成功",'url'=>U('Admin/Ad/adList')]);
            }else{
                $this->ajaxReturn(['status'=>-1,'msg'=>"操作失败"]);
            }
    	}
    	$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Ad/adList');
        // 不管是添加还是修改广告 都清除一下缓存
        delFile(RUNTIME_PATH.'html'); // 先清除缓存, 否则不好预览
        \think\Cache::clear();
    	if($r){
    	    $redirect_url = session("ad_request_url");
            $img_url= session('img_url');
    	    $redirect_url && $this->success("操作成功",U('Admin/Ad/editAd' , ['request_url'=>$redirect_url,'pid'=>$data['pid'],'img_url'=>$img_url]));
    		$this->success("操作成功",U('Admin/AdText/adList'));
    	}else{
    		$this->error("操作失败",$referurl);
    	}
    }
    
    public function notice(){       
        $act = I('get.act','add');
        $id = I('get.id/d');
        $is_top = I('get.is_top/d');//是否APP广告

        $ad_info = array();
        if($id){
            $ad_info = D('notice')->where('id',$id)->find();  
        }

        $this->assign('info',$ad_info);
        $this->assign('act',$act);
        return $this->fetch();
    }
    
    public function noticeList(){
        delFile(RUNTIME_PATH.'html'); // 先清除缓存, 否则不好预览
        $Ad =  M('notice');         
        $keywords = I('keywords/s',false,'trim');
        if(I('is_top') !== ''){
            $where['is_top'] = I('is_top');
            $is_top = I('is_top');
        }

        if($keywords){
            $where['title'] = array('like','%'.$keywords.'%');
        }
        $count = $Ad->where($where)->count();// 查询满足要求的总记录数
        $Page = $pager = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $res = $Ad->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        // $res = $Ad->where($where)->limit(2)->select();
        // var_dump($res);
        // exit;                    
        $show = $Page->show();// 分页显示输出
        $this->assign('list',$res);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$pager);     
        $this->assign('ad_top',$is_top);     

        //判断API模块存在
        if(is_dir(APP_PATH."/api")) $this->assign('is_exists_api',1);
        return $this->fetch();
    }
    

    
    public function noticeHandle(){
        $data = I('post.');
        $data['logTime'] = time();
        
        if($data['act'] == 'add'){
            $r = D('notice')->add($data);
        }
        if($data['act'] == 'edit'){
            $r = D('notice')->where('id', $data['id'])->save($data);
        }
        
        if($data['act'] == 'del'){
            $r = D('notice')->where('id', $data['id'])->delete();
            if($r){
                $this->ajaxReturn(['status'=>1,'msg'=>"操作成功",'url'=>U('Admin/Ad/noticeList')]);
            }else{
                $this->ajaxReturn(['status'=>-1,'msg'=>"操作失败"]);
            }
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/AdText/noticeList');
       
        if($r){
            $this->success("操作成功",U('Admin/AdText/noticeList'));
        }else{
            $this->error("操作失败",$referurl);
        }
    }
}